package org.linlinjava.litemall.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.linlinjava.litemall.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallAdmin;
import org.linlinjava.litemall.db.service.LitemallIncomeService;
import org.linlinjava.litemall.db.vo.AdminIncomeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/income")
@Validated
public class AdminIncomeController {
    private final Log logger = LogFactory.getLog(AdminIncomeController.class);

    @Autowired
    private LitemallIncomeService incomeService;

    @RequiresPermissions("admin:income:list")
    @RequiresPermissionsDesc(menu = {"分销中心", "分销订单"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "")  Integer id,
                       @RequestParam(defaultValue = "")  Integer userId,
                       @RequestParam(defaultValue = "")  Integer incomeUserId,
                       @RequestParam(defaultValue = "")  Integer disType,
                       @RequestParam(defaultValue = "")  Integer disLevel,
                       @RequestParam(defaultValue = "")  Integer status,
                       @RequestParam(defaultValue = "")  Byte orderSn,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "update_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<AdminIncomeVo> userList = incomeService.queryAdminList(id, userId, incomeUserId,disType,disLevel, status,orderSn,page,limit, sort, order);
        return ResponseUtil.okList(userList);
    }

    @RequiresPermissions("admin:income:update")
    @RequiresPermissionsDesc(menu = {"分销中心", "分销订单"}, button = "入账")
    @PostMapping("/update")
    public Object update(@RequestBody AdminIncomeVo user) {
        Subject currentUser = SecurityUtils.getSubject();
        LitemallAdmin admin = (LitemallAdmin) currentUser.getPrincipal();
        int code = incomeService.updateAccount(admin.getUsername(),user);
        if(code == 0){
            return ResponseUtil.ok(code);
        }else {
            return ResponseUtil.fail(code,"请不要重复入账！");
        }

    }
}
